<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RGB 转 Hex 颜色转换器</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f0f4f8;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            width: 400px;
            padding: 30px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        h1 {
            font-size: 24px;
            margin-bottom: 20px;
            color: #333;
        }

        .input-group {
            margin-bottom: 20px;
        }

        .input-group label {
            display: block;
            font-size: 16px;
            color: #555;
            margin-bottom: 8px;
        }

        .input-group input {
            width: 80%;
            padding: 10px;
            font-size: 16px;
            margin: 5px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
            outline: none;
        }

        .input-group input:focus {
            border-color: #66ccff;
        }

        button {
            padding: 10px 20px;
            background-color: #66ccff;
            border: none;
            border-radius: 5px;
            color: white;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #3399cc;
        }

        .output {
            margin-top: 20px;
            font-size: 18px;
            color: #333;
        }

        .output p {
            margin: 10px 0;
        }

        .color-preview {
            margin-top: 20px;
            padding: 20px;
            border-radius: 10px;
            color: #fff;
            font-weight: bold;
            font-size: 18px;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>RGB 转 Hex 颜色转换器</h1>

        <div class="input-group">
            <label for="r">红色 (R):</label>
            <input type="number" id="r" min="0" max="255" value="255">
        </div>
        <div class="input-group">
            <label for="g">绿色 (G):</label>
            <input type="number" id="g" min="0" max="255" value="0">
        </div>
        <div class="input-group">
            <label for="b">蓝色 (B):</label>
            <input type="number" id="b" min="0" max="255" value="0">
        </div>

        <button id="convertBtn">转换为 Hex</button>

        <div class="output">
            <p>Hex 颜色代码: <span id="hexColor">#ff0000</span></p>
        </div>

        <div class="color-preview" id="colorPreview" style="background-color: #ff0000;">
            预览颜色
        </div>
    </div>

    <script>
        // RGB 转 Hex 函数
        const RGBToHex = (r, g, b) =>
            ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');

        // 获取 DOM 元素
        const rInput = document.getElementById('r');
        const gInput = document.getElementById('g');
        const bInput = document.getElementById('b');
        const hexColorDisplay = document.getElementById('hexColor');
        const colorPreview = document.getElementById('colorPreview');
        const convertBtn = document.getElementById('convertBtn');

        // 按钮点击事件监听
        convertBtn.addEventListener('click', () => {
            const r = parseInt(rInput.value);
            const g = parseInt(gInput.value);
            const b = parseInt(bInput.value);

            // 将 RGB 转换为 Hex
            const hexColor = `#${RGBToHex(r, g, b)}`;

            // 更新 Hex 颜色代码和颜色预览
            hexColorDisplay.textContent = hexColor;
            colorPreview.style.backgroundColor = hexColor;
        });
    </script>

</body>

</html>